package day08.thread;


/**
 * @Auther: sky
 * @Date: 2019/3/2 22:16
 * @Description:
 */
public class MyThread extends Thread {
    private int count = 5;

    /**
     * synchronized修饰方法表示该方法同步安全，互斥区
     */
    @Override
    synchronized public void run() {
        super.run();
        count--;
        System.out.println(this.currentThread().getName() + "计算得出count=" + count);
        System.out.println(this.currentThread().getId() + "计算得出count=" + count);
        System.out.println(this.currentThread().getName() + "计算得出count=" + count);
        System.out.println(this.currentThread().getName() + "计算得出count=" + count);
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
        Thread thread = new Thread(myThread, "A");
        try {


            Thread thread1 = new Thread(myThread, "B");
            Thread C = new Thread(myThread, "C");
            Thread thread3 = new Thread(myThread, "D");
            Thread thread4 = new Thread(myThread, "E");

            System.out.println(thread.isAlive());
            thread.start();
            System.out.println(thread.isAlive());

            thread.sleep(2000);

//        thread1.start();
//        thread2.start();
//        thread3.start();
//        thread4.start();
            thread.yield();
            System.out.println("--------------");
            thread1.start();
            System.out.println(thread1.getId());

        } catch (Exception e) {

        }

    }
}
